package com.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beans.Company;
import com.beans.Position;
import com.beans.User;
import com.dao.UserDao;

public class UserDaoImpl extends UniversalDaoImpl implements UserDao {

	public List getListUser(String rollerCode) {
		String sql;
		sql="select roller_code,loginName,name,pwd,sex,email,region_treeCode,address,qq,interest,description,ip,loginTimes,enabled,registTime from user where roller_code='"+rollerCode+"'";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public User login(String name, String pwd) {
		User u=null;
		String sql="from com.beans.User where loginName=? and pwd=?";
		Object[] p=new Object[]{name,pwd};
		List l=this.getHibernateTemplate().find(sql, p);
		if(l!=null&&l.size()>0)
			u=(User)l.get(0);
		return u;
	}

	public com.vo.Page list(int pageNo, int pageSize, User user) {
		String sql="select id,loginName,realName,age,height,pwd,dbo.getCodeName('2',sex) sex,dbo.getCodeName('4',nation) nation," +
				"dbo.getCodeName('3',degree) degree,dbo.getCodeName('0',married) married,pic,dbo.getCodeName('1',region_treeCode) region_treeCode," +
				"dbo.getCodeName('7',major) major,school,workYear,dbo.getCodeName('zw',position) position,wages,dbo.getCodeName('5',workType) workType,tel,phone,qq,email,address,summary," +
				"views,ip,loginTimes,enabled,registTime from j_user where 1=1";
		Map param=new HashMap();
		if(user.getRealName()!=null&&!"".equals(user.getRealName()))param.put("realName like ?", "%"+user.getRealName()+"%");
		if(user.getSex()!=null&&!"".equals(user.getSex()))param.put("sex=?", user.getSex());
		if(user.getWorkType()!=null&&!"".equals(user.getWorkType()))param.put("workType=?", user.getWorkType());
		if(user.getDegree()!=null&&!"".equals(user.getDegree()))param.put("degree like ?", user.getDegree()+"%");
		if(user.getWorkYear()>0)param.put("workYear=?", user.getWorkYear());
		return Page(pageNo, pageSize, sql,"id", param);
	}
	public Map getUser(int id) {
		String sql="select id,loginName,realName,age,height,pwd,dbo.getCodeName('2',sex) sex,dbo.getCodeName('4',nation) nation," +
				"dbo.getCodeName('3',degree) degree,dbo.getCodeName('0',married) married,pic,dbo.getCodeName('1',region_treeCode) region_treeCode," +
				"dbo.getCodeName('7',major) major,school,workYear,dbo.getCodeName('zw',position) position,wages,dbo.getCodeName('5',workType) workType,tel,phone,qq,email,address,summary," +
				"views,ip,loginTimes,enabled,registTime from j_user where id=?";
		return getMap(sql, new Object[]{id});
	}

	public com.vo.Page myFocusCompany(int pageNo, int pageSize, int userid,
			Company c) {
		String sql="select c.* from j_company c where c.id in(select distinct companyid from j_user_to_company_history where userid="+userid+")";
		Map param=new HashMap();
		if(c.getcName()!=null&&!"".equals(c.getcName()))param.put("cname like ?", "%"+c.getcName()+"%");
		return Page(pageNo, pageSize, sql,"cname", param);
	}

	public com.vo.Page myFocusPosition(int pageNo, int pageSize, int userid,
			Position p) {
		String sql="select * from (select distinct p.id,p.companyID,c.cname company,dbo.getCodeName('zw',name) name,dbo.getCodeName('1',region) region,dbo.getCodeName('2',sex) sex,wages,dbo.getCodeName('5',workType) workType,persons,days,p.createtime,detail,orderid,p.views from j_position p left join j_company c on p.companyID=c.id,j_user_to_position_history h where h.positionid=p.id and h.userid="+userid+" ) t";
		Map param=new HashMap();
		if(p.getDetail()!=null&&!"".equals(p.getDetail()))param.put("detail like ?", "%"+p.getDetail()+"%");
		if(p.getRegion()!=null&&!"".equals(p.getRegion()))param.put("region like ?", p.getRegion()+"%");
		if(p.getName()!=null&&!"".equals(p.getName()))param.put("name=?", p.getName());
		if(p.getCompany()!=null)param.put("companyId=?", p.getCompany().getId());
		if(p.getId()>0)param.put("p.id=?", p.getId());
		return Page(pageNo, pageSize, sql,"name", param);
	}

	public boolean myPwd(int userid, String oldPwd, String newPwd) {
		String sql="update j_user set pwd=?,pwd1=pwd,pwd2=pwd1,pwd3=pwd2 where id=? and pwd=?";
		int t=getJdbcTemplate().update(sql,new Object[]{newPwd,userid,oldPwd});
		if(t==1)
			return true;
		return false;
	}

	
}